package com.yourname.store;

import com.yourname.custom.*;
import com.yourname.entity.*;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class DataStore {
    private static CustomStudentList students=new CustomStudentList();
    private static CustomTeacherList teachers=new CustomTeacherList();
    private static CustomCourseList courses=new CustomCourseList();
    private static CustomCourseStudentList courseStudents=new CustomCourseStudentList();

    private static CustomScoreStudentList scoreStudents=new CustomScoreStudentList();

    public static void addScore(StudentScore score){
        if(scoreStudents.add(score)){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    public static void addStudent(Student student){
        if(students.add(student)){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
    public static void addTeacher(Teacher teacher){
        if(teachers.add(teacher)){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
    public static void addCourse(Course course){
        if(courses.add(course)){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
    public static Student getStudent(String s){
        return students.get(s);
    }
    public static Teacher getTeacher(String s){
        return teachers.get(s);
    }
    public static Course getCourse(String s){
        return courses.get(s);
    }
    public static List<StudentScore> getStudentScore(String sid){
        List<StudentScore> studentScores=new ArrayList<>();
        for (StudentScore ss:scoreStudents){
            if (ss.getSid().equals(sid)){
                studentScores.add(ss);
            }
        }
        return studentScores;
    }
    public static StudentScore getStudentScore(String sid,String cid){
        for (StudentScore ss:scoreStudents){
            if (ss.getSid().equals(sid)&&ss.getCid().equals(cid)){
                return ss;
            }
        }
        return null;
    }

    public static CustomScoreStudentList getAllScoreStudent(){
        return scoreStudents;
    }
    public static Course updateCourse(Course newCourse){
        return courses.set(newCourse);
    }
    public static Teacher updateTeacher(Teacher newTeacher){
        return teachers.set(newTeacher);
    }
    public static Student updateStudent(Student newStudent){
        return students.set(newStudent);
    }
    public static CustomCourseStudentList getAllCourseStudent(){
        return courseStudents;
    }
    public static CustomCourseList getAllCourse(){
        return courses;
    }
    public static CustomTeacherList getAllTeacher(){
        return teachers;
    }
    public static CustomStudentList getAllStudent(){
        return students;
    }
    public static CourseStudent getCourseStudentBySid(String sid){
        return courseStudents.getBySid(sid);
    }
    public static CourseStudent getCourseStudentByAll(CourseStudent courseStudent){
        return courseStudents.getByAll(courseStudent);
    }
    public static CourseStudent getCourseStudentByCid(String cid){
        return courseStudents.getByCourseId(cid);
    }

    public static void addCourseStudent(CourseStudent courseStudentBySid) {
        if(courseStudents.add(courseStudentBySid)){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    public static void removeCourseStudent(CourseStudent log) {
        if(courseStudents.remove(log)){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

    /**
     * 将内存中学生的数据存储到文件中
     */
    private static void saveStudents(){
        File f=new File("student.txt");
        try {
            OutputStream os=new FileOutputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            for (Student s:students){
                sBuilder.append(s.getId()).append(",");
                sBuilder.append(s.getName()).append(",");
                sBuilder.append(s.getAge()).append(",");
                sBuilder.append(s.getPassword()).append("\n");
            }
            os.write(sBuilder.toString().getBytes());
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将内存中的教师数据存储到文件中
     */
    private static void saveTeachers(){
        File f=new File("teacher.txt");
        try {
            OutputStream os=new FileOutputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            for (Teacher s:teachers){
                sBuilder.append(s.getId()).append(",");
                sBuilder.append(s.getName()).append(",");
                sBuilder.append(s.getAge()).append(",");
                sBuilder.append(s.getPassword()).append("\n");
            }
            os.write(sBuilder.toString().getBytes());
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将内存中存储的课程信息存储到文件中
     */
    private static void saveCourses(){
        File f=new File("course.txt");
        try {
            OutputStream os=new FileOutputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            for (Course s:courses){
                sBuilder.append(s.getId()).append(",");
                sBuilder.append(s.getName()).append(",");
                sBuilder.append(s.getTid()).append("\n");
            }
            os.write(sBuilder.toString().getBytes());
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将内存中的学生-成绩关系信息存入到文件中
     */
    private static void saveStudentScore(){
        File f=new File("student-score.txt");
        try {
            OutputStream os=new FileOutputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            for (StudentScore s:scoreStudents){
                sBuilder.append(s.getId()).append(",");
                sBuilder.append(s.getSid()).append(",");
                sBuilder.append(s.getCid()).append(",");
                sBuilder.append(s.getGPA()).append(",");
                sBuilder.append(s.getScore()).append(",");
                sBuilder.append(s.isHanging()).append("\n");
            }
            os.write(sBuilder.toString().getBytes());
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将内存中的课程-学生数据存入到文件中
     */
    private static void saveCourseStudent(){
        File f=new File("course-student.txt");
        try {
            OutputStream os=new FileOutputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            for (CourseStudent s:courseStudents){
                sBuilder.append(s.getSid()).append(",");
                sBuilder.append(s.getCid()).append("\n");
            }
            os.write(sBuilder.toString().getBytes());
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 从文件中的课程-学生信息读取到内存中
     */
    private static void loadCourseStudent(){
        File f=new File("course-student.txt");
        try {
            if (!f.exists()){
                f.createNewFile();
                return;
            }
            InputStream os=new FileInputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            byte[] bytes=new byte[os.available()];
            if (os.available()==0){
                return;
            }
            os.read(bytes);
            String str=new String(bytes);
            String[] split = str.split("\n");
            for (String e:split){
                String[] split1 = e.split(",");
                CourseStudent courseStudent=new CourseStudent();
                courseStudent.setSid(split1[0]);
                courseStudent.setCid(split1[1]);
                courseStudents.add(courseStudent);
            }
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 从文件中的学生-成绩信息读取到内存中
     */
    private static void loadStudentScore(){
        File f=new File("student-score.txt");
        try {
            if (!f.exists()){
                f.createNewFile();
                return;
            }
            InputStream os=new FileInputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            byte[] bytes=new byte[os.available()];
            if (os.available()==0){
                return;
            }
            os.read(bytes);
            String str=new String(bytes);
            String[] split = str.split("\n");
            for (String e:split){
                String[] split1 = e.split(",");
                StudentScore studentScore=new StudentScore();
                studentScore.setId(split1[0]);
                studentScore.setSid(split1[1]);
                studentScore.setCid(split1[2]);
                studentScore.setGPA(new BigDecimal(split1[3]));
                studentScore.setScore(Integer.parseInt(split1[4]));
                studentScore.setHanging(Boolean.parseBoolean(split1[5]));
                scoreStudents.add(studentScore);
            }
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 从文件中的学生信息读取到内存中
     */
    private static void loadStudents(){
        File f=new File("student.txt");
        try {
            if (!f.exists()){
                f.createNewFile();
                return;
            }
            InputStream os=new FileInputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            if (os.available()==0){
                return;
            }
            byte[] bytes=new byte[os.available()];
            os.read(bytes);
            String str=new String(bytes);
            String[] split = str.split("\n");
            for (String e:split){
                String[] split1 = e.split(",");
                Student student=new Student();
                student.setId(split1[0]);
                student.setName(split1[1]);
                student.setAge(Integer.parseInt(split1[2]));
                student.setPassword(split1[3]);
                students.add(student);
            }
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 从文件中读取课程信息到内存中
     */
    private static void loadCourses(){
        File f=new File("course.txt");
        try {
            if (!f.exists()){
                f.createNewFile();
                return;
            }
            InputStream os=new FileInputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            byte[] bytes=new byte[os.available()];
            if (os.available()==0){
                return;
            }
            os.read(bytes);
            String str=new String(bytes);
            String[] split = str.split("\n");
            for (String e:split){
                String[] split1 = e.split(",");
                Course course=new Course();
                course.setId(split1[0]);
                course.setName(split1[1]);
                course.setTid(split1[2]);
                courses.add(course);
            }
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 从文件中读取教师信息到内存中
     */
    private static void loadTeachers(){
        File f=new File("teacher.txt");
        try {
            if (!f.exists()){
                f.createNewFile();
                return;
            }
            InputStream os=new FileInputStream(f);
            StringBuilder sBuilder=new StringBuilder();
            byte[] bytes=new byte[os.available()];
            if (os.available()==0){
                return;
            }
            os.read(bytes);
            String str=new String(bytes);
            String[] split = str.split("\n");
            for (String e:split){
                String[] split1 = e.split(",");
                Teacher teacher=new Teacher();
                teacher.setId(split1[0]);
                teacher.setName(split1[1]);
                teacher.setAge(Integer.parseInt(split1[2]));
                teacher.setPassword(split1[3]);
                teachers.add(teacher);
            }
            os.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将所有内存中的数据保存到文件中
     */
    public static void saveData(){
        saveStudentScore();
        saveCourseStudent();
        saveCourses();
        saveTeachers();
        saveStudents();
    }

    /**
     * 从文件加载所有的数据到内存中
     */
    public static void loadData(){
        loadStudents();
        loadCourses();
        loadTeachers();
        loadCourseStudent();
        loadStudentScore();
    }

}
